package com.elong.spring.lifecycle;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanLifeCycle {

    private static void LifeCycleInBeanFactory(){
	Resource res = new ClassPathResource("conf/core/elong_common-servlet.xml");
	BeanFactory bf = new XmlBeanFactory(res);
	
	//向容器中注册MyBeanPostProcessor
	((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor());
	((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
	
	CarLifeCycle car1 = (CarLifeCycle) bf.getBean("carlifecycle");
	car1.introduce();
	car1.setColor("红色");
	
	//第二次拿取，直接从缓存池中获取
	CarLifeCycle car2 = (CarLifeCycle) bf.getBean("carlifecycle");
	
	//查看car1和car2是否指向同一引用
	System.out.println("car1==car2: " + (car1 == car2));
	
	//关闭容器
	((XmlBeanFactory)bf).destroySingletons();
    }
    
    public static void main(String[] args) {
	LifeCycleInBeanFactory();
    }
}
